﻿using GameTools.Classes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace GameTools.Models
{
    [Serializable]
    public class Script
    {
        /// <summary>
        /// 脚本名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 脚本说明
        /// </summary>
        public string Decscription { get; set; }
        /// <summary>
        /// 脚本详细
        /// </summary>
        public List<ScriptDetail> Details { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }



        public void Save()
        {
            string filePath = Scripts.DIRECTORY_PATH + "\\" + this.Name + ".ini";
            LoliCode.Serialization<Script>(this, filePath);
        }

    }

    [Serializable]
    public class Scripts : List<Script>
    {
        public const string DIRECTORY_PATH = "Scripts";


        public static Scripts Load()
        {
            Scripts scripts = new Scripts();

            if (!Directory.Exists(DIRECTORY_PATH))
            {
                Directory.CreateDirectory(DIRECTORY_PATH);
            }

            foreach (var item in Directory.GetFiles(DIRECTORY_PATH))
            {
                try
                {
                    Script script = LoliCode.Deserialization<Script>(item);
                    scripts.Add(script);
                }
                catch { }
            }
         
            return scripts;

        }

        public void Save()
        {


            if (!Directory.Exists(DIRECTORY_PATH))
            {
                Directory.CreateDirectory(DIRECTORY_PATH);
            }

            foreach (var item in Directory.GetFiles(DIRECTORY_PATH))
            {
                File.Delete(item);
            }

            foreach (var item in this)
            {
                item.Save();
            }
        }

    }
}
